<?php
/**
 * 类型控制器
 */
class TypeController extends AuthController {

	private $model;

	public function __init() {
		$this -> model = K('Type');
	}

	public function index() {
		//获得所有类型的所有条数
		$count = $this -> model -> count();
		$page = new Page($count, 15, 10);
		$this -> assign('types', $this -> model -> typeList($page -> limit()));
		$this -> assign('page', $page -> show(2));
		$this -> display();
	}
	
	/**
	 * 删除类型方法
	 */
	 public function del(){
	 	$tid = Q('get.tid');
		if($this->model->delType($tid)){
			//删除成功后跳转到类型列表首页
			$this->success('删除类型成功',U('Type/index'));
		}
	 }
	
	
	
	
	/**
	 * 修改类型方法
	 */
	 public function edit(){
	 	$tid = Q('get.tid');
		if (IS_POST) {
			
			if ($this -> model -> editType()) {
				$this -> success('修改类型成功', U('Type/index'));
			} else {
				$this -> error($this -> model -> error);
			}
		}
		$this->assign('type',$this->model->where('tid='.$tid)->find());
	 	$this->display();
	 }
	
	
	/**
	 * ajax检查类型名称是否存在
	 */
	public function ajaxCheckType() {

		if ($this -> model -> where(Q('post.')) -> find()) {
			$this -> ajax(array('message' => '类型已经存在请更换', 'status' => 0));
		} else {
			$this -> ajax(array('message' => '类型可以添加', 'status' => 1));
		}
	}

	/**
	 * 添加类型方法
	 */
	public function addType() {
		if (IS_POST) {
			
			if ($this -> model -> addType()) {
				$this -> success('添加类型成功', U('Type/index'));
			} else {
				$this -> error($this -> model -> error);
			}
		}
		$this -> display();
	}

}
